import { defineConfig } from "vitepress";
const fileAndStyles: Record<string, string> = {};
export default defineConfig({
  title: "My Awesome Project",
  description: "sdadsd",
  themeConfig: {
    nav: [{ text: "主页", link: "/" }],
    sidebar: [
      {
        text: "组件列表",
        items: [
          { text: "导出功能", link: "/components/export" },
          { text: "上传功能", link: "/components/上传" },
          { text: "复制功能", link: "/components/copy" },
          { text: "图表功能", link: "/components/echart线型图" },
          { text: "地图功能", link: "/components/echart地图" },
          {
            text: "iconify图标",
            link: "/components/iconify",
          },
        ],
      },
    ],

    socialLinks: [{ icon: "gitee", link: "https://gitee.com/jian-bin-zhao" }],
  },
  vite: {
    ssr: {
      noExternal: ["naive-ui", "date-fns", "vueuc"],
    },
  },
  postRender(context) {
    const styleRegex = /<css-render-style>((.|\s)+)<\/css-render-style>/;
    const vitepressPathRegex = /<vitepress-path>(.+)<\/vitepress-path>/;
    const style = styleRegex.exec(context.content)?.[1];
    const vitepressPath = vitepressPathRegex.exec(context.content)?.[1];
    if (vitepressPath && style) {
      fileAndStyles[vitepressPath] = style;
    }
    context.content = context.content.replace(styleRegex, "");
    context.content = context.content.replace(vitepressPathRegex, "");
  },
  transformHtml(code, id) {
    const html = id.split("/").pop();
    if (!html) return;
    const style = fileAndStyles[`/${html}`];
    if (style) {
      return code.replace(/<\/head>/, `${style}</head>`);
    }
  },
});
